package cn.hfjava.kz.listener;

import cn.hfjava.kz.entity.excel.ExamAdmissionExcel;
import cn.hfjava.kz.service.ExamAdmissionTicketService;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;

import java.util.ArrayList;
import java.util.List;

public class ExamAdmissionListener implements ReadListener<ExamAdmissionExcel> {
    public static String count;
    private ExamAdmissionTicketService examAdmissionTicketService;
    private List<ExamAdmissionExcel> examAdmissionExcelList = new ArrayList<>();
    public ExamAdmissionListener(ExamAdmissionTicketService examAdmissionTicketService) {
        this.examAdmissionTicketService = examAdmissionTicketService;

    }


    @Override
    public void invoke(ExamAdmissionExcel examAdmissionExcel, AnalysisContext analysisContext) {
        examAdmissionExcelList.add(examAdmissionExcel);
        if (examAdmissionExcelList.size() >= 1000) {
            this.examAdmissionTicketService.saveExam(examAdmissionExcelList);
            examAdmissionExcelList.clear();
        }

    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        this.examAdmissionTicketService.saveExam(examAdmissionExcelList);
        examAdmissionExcelList.clear();
    }
}
